package com.mingmen.supplier.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.mingmen.supplier.R;
import com.mingmen.supplier.activity.LoginActivity;
import com.mingmen.supplier.activity.NewOrderDetailActivity;
import com.mingmen.supplier.adapter.AuditListAdapter;
import com.mingmen.supplier.adapter.DingDanDaiChuLiListAdapter;
import com.mingmen.supplier.adapter.DingDanListAdapter;
import com.mingmen.supplier.base.BaseBean;
import com.mingmen.supplier.flycotablelayout.flyco.tablayout.SegmentTabLayout;
import com.mingmen.supplier.http.callback.StringCallback;
import com.mingmen.supplier.httpconfig.HttpResponse;
import com.mingmen.supplier.model.DingListModel;
import com.mingmen.supplier.refresh.ClassicsFooter;
import com.mingmen.supplier.refresh.ClassicsHeader;
import com.mingmen.supplier.util.GsonUtil;
import com.mingmen.supplier.util.SharePreferenceUtil;
import com.mingmen.supplier.util.ViewFindUtils;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import de.greenrobot.event.EventBus;
import okhttp3.Call;
import okhttp3.Response;

@SuppressLint("ValidFragment")
public class DingDanListFragment extends BaseFragment implements DingDanDaiChuLiListAdapter.OnItemClickListener {
    @BindView(R.id.card_title_tv)
    TextView cardTitleTv;
    Unbinder unbinder;
    private String mTitle;


    public static DingDanListFragment getInstance(String title) {
        DingDanListFragment sf = new DingDanListFragment();
        sf.mTitle = title;
        return sf;
    }

    LinearLayoutManager linearLayoutManager;
    @BindView(R.id.tl_4)
    SegmentTabLayout tabLayout_4;
    private ArrayList<Fragment> mFragments2 = new ArrayList<>();
    private String[] mTitles_2 = {"未处理", "已拒绝"};
    @BindView(R.id.recycle_shopo)
    RecyclerView mRecyclerView;
    DingDanDaiChuLiListAdapter commodityAdapter;

    @BindView(R.id.text_weichuli)
    TextView text_weichuli;
    int flag = 0;
    @BindView(R.id.layout_all)
    LinearLayout layout_all;
    @BindView(R.id.text_yijujue)
    TextView text_yijujue;
    String token;

    List<DingListModel.DataBean> dataBeanList = new ArrayList<>();
    @BindView(R.id.layout_nodata)
    LinearLayout layout_nodata;
    @BindView(R.id.refreshLayout)
    RefreshLayout refreshLayout;
    int pageNumber = 1;
    int totalPage;
    @BindView(R.id.layout_alldata)
    LinearLayout layout_alldata;

    @Override
    public View initView() {
        View v = LayoutInflater.from(getActivity()).inflate(R.layout.fr_dinglist, null);
        unbinder = ButterKnife.bind(this, v);
        EventBus.getDefault().register(this);
        TextView card_title_tv = (TextView) v.findViewById(R.id.card_title_tv);
        card_title_tv.setText(mTitle);
        token = (String) SharePreferenceUtil.get(getActivity(), "token", "");

        linearLayoutManager = new LinearLayoutManager(getActivity());

        mRecyclerView.setLayoutManager(linearLayoutManager);
        commodityAdapter = new DingDanDaiChuLiListAdapter(getActivity(), dataBeanList);
        mRecyclerView.setAdapter(commodityAdapter);
        commodityAdapter.setOnItemClickListener(this);
        mRecyclerView.setVisibility(View.VISIBLE);

        getData(token, "4", "", "", "", pageNumber);
        refreshLayout.setRefreshHeader(new ClassicsHeader(getActivity()));
        refreshLayout.setRefreshFooter(new ClassicsFooter(getActivity()));

        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                if (dataBeanList.size() > 0) {
                    dataBeanList.clear();
                }
                pageNumber = 1;

//                refreshlayout.finishRefresh(2000/*,false*/);//传入false表示刷新失败
                getData(token, "4", "", "", "", pageNumber);
            }
        });

        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {
                if (pageNumber >= totalPage) {
                    Toast.makeText(getActivity(), "已到最后一页", Toast.LENGTH_SHORT).show();
                    refreshlayout.finishLoadMore(false);//传入false表示加载失败
                } else {
                    pageNumber++;

                    getData(token, "4", "", "", "", pageNumber);
                    HttpResponse.listOrder(token, "4", "", "", "", pageNumber, new StringCallback(getActivity()) {
                        @Override
                        public void onSuccess(String s, Call call, Response response) {

                            BaseBean baseBean = new BaseBean();
                            baseBean = GsonUtil.GsonToBean(s, BaseBean.class);
                            if (baseBean.getCode() == 401) {
                                startActivity(new Intent(getActivity(), LoginActivity.class));
                                getActivity().finish();
                            } else {
                                DingListModel dingListModel = new DingListModel();
                                dingListModel = GsonUtil.GsonToBean(s, DingListModel.class);
                                dataBeanList.addAll(dingListModel.getData());
                                if (dataBeanList.size() == 0) {
                                    layout_nodata.setVisibility(View.VISIBLE);
                                    layout_alldata.setVisibility(View.GONE);
                                } else {
                                    layout_nodata.setVisibility(View.GONE);
                                    layout_alldata.setVisibility(View.VISIBLE);
                                }
                                totalPage = dingListModel.getTotalPage();
                                commodityAdapter.setDataList(dataBeanList);
                            }

                        }

                        @Override
                        public void onAfter(String s, Exception e) {
                            super.onAfter(s, e);
                            refreshLayout.finishRefresh(100/*,false*/);//传入false表示刷新失败
                            refreshLayout.finishLoadMore(100/*,false*/);//传入false表示加载失败
                        }
                    });
                    refreshlayout.finishLoadMore(100/*,false*/);//传入false表示加载失败
                }


            }
        });
        commodityAdapter.setOnItemClickListener(this);


        return v;
    }

    @Override
    public void initData() {

    }

    public void getData(String token, String st, String keyword, String startTime, String endTime, int pageNumber) {
        HttpResponse.listOrder(token, st, keyword, startTime, endTime, pageNumber, new StringCallback(getActivity()) {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                if (dataBeanList.size() > 0) {
                    dataBeanList.clear();
                }
                BaseBean baseBean = new BaseBean();
                baseBean = GsonUtil.GsonToBean(s, BaseBean.class);
                if (baseBean.getCode() == 401) {
                    startActivity(new Intent(getActivity(), LoginActivity.class));
                    getActivity().finish();
                } else {
                    DingListModel dingListModel = new DingListModel();
                    dingListModel = GsonUtil.GsonToBean(s, DingListModel.class);
                    dataBeanList.addAll(dingListModel.getData());
                    if (dataBeanList.size() == 0) {
                        layout_nodata.setVisibility(View.VISIBLE);
                        layout_alldata.setVisibility(View.GONE);
                    } else {
                        layout_nodata.setVisibility(View.GONE);
                        layout_alldata.setVisibility(View.VISIBLE);
                    }
                    totalPage = dingListModel.getTotalPage();
                    commodityAdapter.setDataList(dataBeanList);
                }

            }

            @Override
            public void onAfter(String s, Exception e) {
                super.onAfter(s, e);
                refreshLayout.finishRefresh(100/*,false*/);//传入false表示刷新失败
                refreshLayout.finishLoadMore(100/*,false*/);//传入false表示加载失败
            }
        });

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


    @OnClick({R.id.card_title_tv, R.id.text_weichuli, R.id.text_yijujue})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.text_weichuli:
                layout_all.setBackground(getActivity().getResources().getDrawable(R.mipmap.weichuolileft));
                text_yijujue.setTextColor(getActivity().getResources().getColor(R.color.normal_bluetextcolor));
                text_weichuli.setTextColor(getActivity().getResources().getColor(R.color.white));

                getData(token, "4", "", "", "", pageNumber);
                flag = 1;
                break;
            case R.id.text_yijujue:
                flag = 0;
                layout_all.setBackground(getActivity().getResources().getDrawable(R.mipmap.weichuoliright));


                text_weichuli.setTextColor(getActivity().getResources().getColor(R.color.normal_bluetextcolor));
                text_yijujue.setTextColor(getActivity().getResources().getColor(R.color.white));
                getData(token, "4", "", "", "", pageNumber);
                break;
        }

    }


    @Override
    public void onItemClickListener(View view, int pos, List<DingListModel.DataBean> myLiveList) {
//        startActivity(new Intent(getActivity(), NewOrderDetailActivity.class));
        Intent intent = new Intent(getActivity(), NewOrderDetailActivity.class);
        intent.putExtra("billId", myLiveList.get(pos).getBillId() + "");
        intent.putExtra("flag", flag + "");
        startActivity(intent);

    }

    public void onEventMainThread(Integer eventBean) {
        if (eventBean == 7) {
            if (flag==0){
                getData(token, "4", "", "", "", pageNumber);
            }else {
                getData(token, "5", "", "", "", pageNumber);
            }
//            getData(token, "4", "", "", "", pageNumber);
            getData(token, "5", "", "", "", pageNumber);
        } else if (eventBean == 6) {
            if (flag==0){
                getData(token, "4", "", "", "", pageNumber);
            }else {
                getData(token, "5", "", "", "", pageNumber);
            }
        }

    }
}